package com.xk857.simpleeffect.fragment.main;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.google.gson.reflect.TypeToken;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.adapter.ListNoBusinessAdapter;
import com.xk857.simpleeffect.adapter.ListYesBusinessAdapter;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.vo.TodayBusinessVO;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.ListViewUtil;
import com.xk857.simpleeffect.utils.OkHttpUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class ToDoListFragment extends Fragment {
    private View view;
    private CalendarView calendarView;
    private TextView yearMonthText;
    private ListView noBusinessListView;
    private ListView yesBusinessListView;
    private ConstraintLayout yesMainConstraint;
    private ConstraintLayout emptyLayout;
    private TextView unFinishNumberText;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_fragment_todo_list, container, false);
        initView();
        return view;
    }

    private void initView() {
        CalendarLayout calendarLayout = view.findViewById(R.id.calendarLayout);
        calendarView = view.findViewById(R.id.calendarView);
        noBusinessListView = view.findViewById(R.id.no_business_list_view);
        yesBusinessListView = view.findViewById(R.id.yes_business_list_view);
        yesMainConstraint = view.findViewById(R.id.yes_main_constraint);
        emptyLayout = view.findViewById(R.id.empty_layout);
        unFinishNumberText = view.findViewById(R.id.un_finish_number_text);
        initCalendarView();

        yearMonthText = view.findViewById(R.id.year_month_text);
    }

    private void initData() {
        setTopDate(calendarView.getSelectedCalendar(), true);
    }

    // 设置顶部时间文字
    private void setTopDate(Calendar calendar, Boolean showLoading) {
        String calendarText = calendar.getYear() + "年" + calendar.getMonth() + "月";
        yearMonthText.setText(calendarText);
        Type type = new TypeToken<Result<TodayBusinessVO>>() {}.getType();

        String url = OkHttpUtils.BASE_URL + "/api/v1/business/customer/search/" + calendar.getYear() + (calendar.getMonth() < 10 ? "0" : "") + calendar.getMonth() + calendar.getDay();
        OkHttpUtils.getInstance().get(getActivity(), url, type, showLoading, (Consumer<TodayBusinessVO>) vo -> {

            if (getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    unFinishNumberText.setText(String.valueOf(vo.getNoBusinessList().size()));
                    if (vo.getNoBusinessList().size() > 0) {
                        noBusinessListView.setAdapter(new ListNoBusinessAdapter(getActivity(), vo.getNoBusinessList(), aBoolean -> {
                            if (getActivity() != null) {
                                getActivity().runOnUiThread(() -> setTopDate(calendar, false));
                            }
                        }));
                        noBusinessListView.setVisibility(View.VISIBLE);
                        emptyLayout.setVisibility(View.GONE);
                        ListViewUtil.setListViewHeight(noBusinessListView);
                    } else {
                        noBusinessListView.setVisibility(View.GONE);
                        emptyLayout.setVisibility(View.VISIBLE);
                    }

                    if (vo.getYesBusinessList().size() > 0) {
                        yesBusinessListView.setAdapter(new ListYesBusinessAdapter(getActivity(), vo.getYesBusinessList(), aBoolean -> {
                            if (getActivity() != null) {
                                getActivity().runOnUiThread(() -> setTopDate(calendar, false));
                            }
                        }));
                        yesMainConstraint.setVisibility(View.VISIBLE);
                        ListViewUtil.setListViewHeight(yesBusinessListView);
                    } else {
                        yesMainConstraint.setVisibility(View.GONE);
                    }
                });
            }
        });
    }


    private void initCalendarView() {
        Map<String, Calendar> map = new HashMap<>();
        Type type = new TypeToken<Result<ArrayList<String>>>() {}.getType();
        OkHttpUtils.getInstance().get(getActivity(), RequestEnum.BUSINESS_DATE_HAVE, type, true, (Consumer<ArrayList<String>>) strList -> {
            for (String str : strList) {
                String[] dates = str.split("-");
                map.put(getSchemeCalendar(dates[0], dates[1], dates[2], R.color.calender_bottom_point).toString(), getSchemeCalendar(dates[0], dates[1], dates[2], Color.TRANSPARENT));
            }
            calendarView.setSchemeDate(map);
        });

        // 设置右上角红点
        // map.put(getSchemeCalendar(year, month, j, Color.RED).toString(), getSchemeCalendar(year, month, j, Color.RED));
        // 设置底部圆点颜色
        // map.put(getSchemeCalendar(year, month, i, Color.TRANSPARENT).toString(), getSchemeCalendar(year, month, i, Color.TRANSPARENT));

        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                setTopDate(calendar, true);
            }
        });
    }

    public Calendar getSchemeCalendar(String year, String month, String day, int color) {
        return getSchemeCalendar(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day), color);
    }

    public Calendar getSchemeCalendar(int year, int month, int day, int color) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        return calendar;
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
